package com.ysq.track.view.cmd;

import com.ysq.track.util.ScannerUtil;
import com.ysq.track.util.TripleDESCBC;
import org.apache.commons.lang3.CharUtils;

import java.util.ArrayList;
import java.util.List;

public class CommandInputView {
    private List<CommandInput> commandList;

    private TripleDESCBC desUtil;

    public CommandInputView() throws Exception{
        initCommandList();

        desUtil = new TripleDESCBC();
    }

    public void initCommandList() {
        commandList = new ArrayList<CommandInput>();

        //query all
        CommandInput cmdHelp = new CommandInput('H', "帮助[Help]");
        CommandInput cmdEnc = new CommandInput('E', "加密[Encrypt]");
        CommandInput cmdDec = new CommandInput('D', "解密[Decrypt]");
        CommandInput cmdQuit = new CommandInput('Q', "退出[Quit]");

        commandList.add(cmdHelp);
        commandList.add(cmdEnc);
        commandList.add(cmdDec);
        commandList.add(cmdQuit);
    }

    /**
     * 打印命令提示
     */
    public void printOptions() {
        String tabString = "    ";
        System.out.println("Pleasy input the command: ");
        for (CommandInput oneCommand : commandList) {
            char commandChar = oneCommand.getCommandChar();
            String remark = oneCommand.getRemark();

            String info = String.format("%s[%c] : %s", tabString, commandChar, remark);
            System.out.println(info);
        }
        System.out.println("========================================\n");
    }


    /**
     * 处理控制台输入逻辑
     */
    public void processInput() {
        String line = null;

        printOptions();
        quit:
        while (true) {
            line = ScannerUtil.readInput();

            if (line != null && !"".equals(line)) {
                //char inputChar = line.charAt(0);
                char inputChar = CharUtils.toChar(line.toUpperCase());
                switch (inputChar) {
                    case 'H':
                        printOptions();
                        break;
                    case 'E':
                        cmdEncrypt();
                        break;
                    case 'D':
                        cmdDecrypt();
                        break;
                    case 'Q':
                        System.out.println("bye ...");
                        break quit;
                    default:
                        break;
                }

            }else {
                printOptions();
            }


        }

    }


    /**
     * 加密
     * @return
     */
    public int cmdEncrypt() {
        try {
            System.out.println("[ENC]Your text : ");
            String encStr = ScannerUtil.readInput();

            System.out.println("[ENC]Secret key : ");
            String keyStr = ScannerUtil.readPassword();

            try {
                String result = desUtil.encrypt2String(keyStr, null, encStr);
                System.out.println("[ENC]Result text:");
                System.out.println("----------------------------------------");

                System.out.println(result);

                System.out.println("----------------------------------------");
            } catch (Exception e) {
                System.out.println("[ENC]Error");
            }
        } catch (Exception e) {
            System.out.println("input Error.");
        }
        return 1;
    }

    /**
     * 解密
     * @return
     */
    public int cmdDecrypt() {
        try {
            System.out.println("[DEC]Your ciphertext : ");
            String ciphertext = ScannerUtil.readInput();

            System.out.println("[DEC]Secret key : ");
            String keyStr = ScannerUtil.readPassword();

            try {
                String result = desUtil.decrypt2String(keyStr, null, ciphertext);
                System.out.println("[DEC]Result text:");
                System.out.println("----------------------------------------");

                System.out.println(result);

                System.out.println("----------------------------------------");
            } catch (Exception e) {
                System.out.println("[DEC]Error:The secret key and the ciphertext does not match");
            }

        } catch (Exception e) {
            System.out.println("input Error.");
        }
        return 1;
    }

    /**
     * 退出
     * @return
     */
    public int cmdQuit() {

        return 0;
    }
}
